package com.iplant.ams.controller.ams.fxt;

import com.iplant.ams.service.AMSService;
import com.iplant.ams.service.basicenum.ams.AMSFixedAssetsType;
import com.iplant.ams.service.po.ams.asset.AMSAssetClassification;
import com.iplant.ams.service.po.ams.asset.AMSAssetStandardModel;
import com.iplant.ams.service.po.ams.fxt.AMSMeasureTool;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.annotation.trace.TraceAPI;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.orm.ORMUtils;
import com.iplant.base.utils.sql.orm.PoService;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/AMSMeasureTool")
public class AMSMeasureToolController extends BasicController<AMSMeasureTool> {
    private static final Logger logger = LoggerFactory.getLogger(AMSMeasureToolController.class);


    @Autowired
    public AMSService mAMSService;

    PoService<AMSAssetClassification> AssetClassificationService;
    PoService<AMSAssetStandardModel> AssetStandardModelService;

    public AMSMeasureToolController() {
        super(AMSMeasureTool.class);

        AssetClassificationService=ORMUtils.GetPoService(AMSAssetClassification.class);
        AssetStandardModelService=ORMUtils.GetPoService(AMSAssetStandardModel.class);
    }

    /**
     * 条件查询
     */
    @GetMapping("/InitAll")
    public Object InitAll(HttpServletRequest request) {
        Object wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            Map<String, Object> wParams = GetParamsMap(request);
            Pagination wPagination = GetPagination(request);

            //初始化量器具数据
            mAMSService.AMS_InitMeasureTool(BMSEmployee.SysAdmin);

            ServiceResult<List<AMSMeasureTool>> wServiceResult =
                    mPoService.SelectPOList(wLoginUser, wParams, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }




    /**
     * 新增或更新
     */
    @TraceAPI(value = "新增或更新")
    @PostMapping("/Update")
    public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            AMSMeasureTool wAMSMeasureTool = CloneTool.Clone(wParam.get("data"), AMSMeasureTool.class);


            AMSAssetClassification wAMSAssetClassification = new AMSAssetClassification();
            wAMSAssetClassification.Name = "量器具";
            wAMSAssetClassification.ID = 1;
            wAMSAssetClassification.TypeEnum = AMSFixedAssetsType.Measure.getValue();
            Map<String, Object> wParasMap = new HashMap<>();
            wParasMap.put("TypeEnum", AMSFixedAssetsType.Measure.getValue());
            ServiceResult<List<AMSAssetClassification>> wAMSAssetClassificationServiceResult = AssetClassificationService
                    .SelectPOList(wLoginUser, wParasMap, Pagination.getNewMaxSize());


            if (StringUtils.isNotEmpty(wAMSAssetClassificationServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wAMSAssetClassificationServiceResult.getFaultCode());
                return wResult;
            }

            if (wAMSAssetClassificationServiceResult != null && wAMSAssetClassificationServiceResult.Result != null
                    && wAMSAssetClassificationServiceResult.Result.size() > 0) {
                wAMSAssetClassification = wAMSAssetClassificationServiceResult.Result.get(0);
            }

            //查询标准型号

            AMSAssetStandardModel wAMSAssetStandardModel;
            if(wAMSMeasureTool.StandardModelID>0){
                ServiceResult<AMSAssetStandardModel> wAssetStandardModelServiceResult=AssetStandardModelService.SelectPO(wLoginUser, wAMSMeasureTool.StandardModelID, "");
                wAMSAssetStandardModel=wAssetStandardModelServiceResult.Result;
                if (StringUtils.isNotEmpty(wAssetStandardModelServiceResult.getFaultCode())) {
                    wResult = GetResult(RetCode.SERVER_CODE_ERR, wAssetStandardModelServiceResult.getFaultCode());
                    return wResult;
                }
                if(wAMSAssetStandardModel==null||wAMSAssetStandardModel.ID<=0){
                    wAMSAssetStandardModel = new AMSAssetStandardModel(wAMSMeasureTool, wAMSAssetClassification);
                }else{
                    wAMSAssetStandardModel.SetModel(wAMSMeasureTool);
                }
            }else{
                wAMSAssetStandardModel = new AMSAssetStandardModel(wAMSMeasureTool, wAMSAssetClassification);
            }
            wAMSAssetStandardModel.setUserInfo(wLoginUser);
            ServiceResult<Integer> wServiceResult = AssetStandardModelService.UpdatePo(wLoginUser, wAMSAssetStandardModel);

            if (StringUtils.isNotEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
                return wResult;
            }

            if (wServiceResult.Result == null || wAMSAssetStandardModel.ID <= 0) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, "插入标准型号失败！");
                return wResult;
            }
            wAMSMeasureTool.StandardModelID = wAMSAssetStandardModel.ID;
            wAMSMeasureTool.Code=wAMSAssetStandardModel.Code;
            wServiceResult = mPoService.UpdatePo(wLoginUser, wAMSMeasureTool);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wAMSMeasureTool);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    /**
     * 新增或更新
     */
    @TraceAPI("批量新增或更新")
    @PostMapping("/UpdateList")
    public Object UpdateList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // ServiceResult<List<AMSAssetStandardModel>> wServiceResult = mPoService.UpdatePoList(wLoginUser, wParam);
            //成功后插入设备型号
            //默认使用设备类型第一个激活的
            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            List<AMSMeasureTool> wList = CloneTool.CloneArray(wParam.get("data"), AMSMeasureTool.class);
            AMSAssetClassification wAMSAssetClassification = new AMSAssetClassification();
            wAMSAssetClassification.Name = "量器具";
            wAMSAssetClassification.ID = 1;
            wAMSAssetClassification.TypeEnum = AMSFixedAssetsType.Measure.getValue();
            Map<String, Object> wParasMap = new HashMap<>();
            wParasMap.put("TypeEnum", AMSFixedAssetsType.Measure.getValue());
            ServiceResult<List<AMSAssetClassification>> wAMSAssetClassificationServiceResult = AssetClassificationService
                    .SelectPOList(wLoginUser, wParasMap, Pagination.getNewMaxSize());

            if (StringUtils.isNotEmpty(wAMSAssetClassificationServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wAMSAssetClassificationServiceResult.getFaultCode());
                return wResult;
            }

            if (wAMSAssetClassificationServiceResult != null && wAMSAssetClassificationServiceResult.Result != null && wAMSAssetClassificationServiceResult.Result.size() > 0) {
                wAMSAssetClassification = wAMSAssetClassificationServiceResult.Result.get(0);
            }


            ServiceResult<Integer> wServiceResult = new ServiceResult<>();
            AMSAssetStandardModel wAMSAssetStandardModel;
            ServiceResult<AMSAssetStandardModel> wAssetStandardModelServiceResult;
            for (AMSMeasureTool wAMSMeasureTool : wList) {
                if(wAMSMeasureTool==null)
                    continue;


                //查询标准型号
                if(wAMSMeasureTool.StandardModelID>0){
                    wAssetStandardModelServiceResult=AssetStandardModelService.SelectPO(wLoginUser, wAMSMeasureTool.StandardModelID, "");
                    wAMSAssetStandardModel=wAssetStandardModelServiceResult.Result;
                    if (StringUtils.isNotEmpty(wAssetStandardModelServiceResult.getFaultCode())) {
                        wResult = GetResult(RetCode.SERVER_CODE_ERR, wAssetStandardModelServiceResult.getFaultCode());
                        return wResult;
                    }
                    if(wAMSAssetStandardModel==null||wAMSAssetStandardModel.ID<=0){
                        wAMSAssetStandardModel = new AMSAssetStandardModel(wAMSMeasureTool, wAMSAssetClassification);
                    }else{
                        wAMSAssetStandardModel.SetModel(wAMSMeasureTool);
                    }
                }else{
                    wAMSAssetStandardModel = new AMSAssetStandardModel(wAMSMeasureTool, wAMSAssetClassification);
                }
                wAMSAssetStandardModel.setUserInfo(wLoginUser);
                wServiceResult = AssetStandardModelService.UpdatePo(wLoginUser, wAMSAssetStandardModel);
                if (StringUtils.isNotEmpty(wServiceResult.getFaultCode())) {
                    break;
                }
                wAMSMeasureTool.StandardModelID=wAMSAssetStandardModel.ID;
                wAMSMeasureTool.Code=wAMSAssetStandardModel.Code;
                wAMSMeasureTool.setUserInfo(wLoginUser);
                wServiceResult = mPoService.UpdatePo(wLoginUser, wAMSMeasureTool);

                if (StringUtils.isNotEmpty(wServiceResult.getFaultCode())) {
                    break;
                }
            }

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "",wList, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

}
